﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Reflection;
using IService.DataDLL.Utility;
using IService.Model;
using IService.Business;
using IService.Utility;

namespace IService.InfoControls
{
    public partial class EditArticle : System.Web.UI.UserControl
    {
        private News _info;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
                _FileBrowser.BasePath = "/ckfinder/";
                _FileBrowser.SetupCKEditor(this.newsContentCKeditor);
                InitPageData();
            }
        }

        protected void CheckAction(string action)
        {
            if (!ParamHandler.IsLegalForString(action) || (action != "add" && action != "edit"))
            {
                Response.Redirect(StringUtil.GetAppConfigString("AdminParamErrorPage"));
            }
        }
        protected void CheckIntType(string param)
        {
            if (!ParamHandler.IsLegalForInt(param))
            {
                Response.Redirect(StringUtil.GetAppConfigString("AdminParamErrorPage"));
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("~/Admin/Pages/ListNews.aspx?$_k=" + new Random().NextDouble());
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void InitPageData()
        {
            CheckAction(Action);
            DataTable table = DTVBusiness.TypeManager.GetTypeListAll();
            this.NewsType.DataSource = table.DefaultView;
            this.NewsType.DataValueField = table.Columns["Id"].ColumnName;
            this.NewsType.DataTextField = table.Columns["TypeName"].ColumnName;
            this.NewsType.DataBind();
            this.NewsType.SelectedIndex = 0;
            if (Action == "edit")
            {
                CheckIntType(NewsID);
                News news = CurrentNews;
                this.newsContentCKeditor.Text = news.NewsContent;
                this.NewsType.SelectedValue = news.NewsTypeId.ToString();
                this.Title.Text = news.NewsTitle;
                this.CreatePeople.Text = news.PublishMan;
            }
        }

        protected void BtnEditArticle_Click(object sender, EventArgs e)
        {
            string newsTitle = this.Title.Text;
            string createPeople = this.CreatePeople.Text;
            if (string.IsNullOrEmpty(newsTitle))
            {
                this.LblError.Text = "<font color='red'>新闻标题不能为空！</font>";
            }
            else if (string.IsNullOrEmpty(createPeople))
            {
                this.LblError.Text = "<font color='red'>发布人不能为空！</font>";
            }
            else
            {
                News news = CurrentNews;
                news.NewsContent = this.newsContentCKeditor.Text;
                news.PublishMan = this.CreatePeople.Text;
                news.NewsTypeId = int.Parse(this.NewsType.SelectedValue);
                news.NewsTitle = this.Title.Text;
                if (Action == "edit")
                {
                    news.ID = int.Parse(NewsID);
                    news.ModifyTime = DateTime.Now;
                }
                else if (Action == "add")
                {
                    news.CreateTime = DateTime.Now;
                }
                bool hassuccess = news.Save();
                if (hassuccess)
                {
                    JsUtil.AlertConfirm("保存成功！", this.Page);
                    Response.Redirect("~/Admin/Pages/ListNews.aspx");
                }
                else
                {
                    JsUtil.Alert("保存失败！", this.Page);
                }
            }
        }

        protected string NewsID
        {
            get
            {
                return base.Request["newsId"];
            }
        }

        protected string Action
        {
            get
            {
                return base.Request["action"];
            }
        }

        protected News CurrentNews
        {
            get
            {
                News info = null;
                if (this._info != null)
                {
                    info = this._info;
                }
                if (info == null)
                {
                    if (ParamHandler.IsLegalForInt(NewsID))
                    {
                        info = DTVBusiness.NewsManager.GetNewsById(int.Parse(NewsID));
                    }
                    else
                    {
                        info = new News();
                    }
                    this._info = info;
                }
                return info;
            }
        }
    }
}